{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6f9dc945-7635-4b51-81b1-8af30fae77f2",
   "metadata": {},
   "source": [
    "# 处理图片\n",
    "\n",
    "PIL是比较流行的图片处理库。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ba1c0669-a4cf-4d0c-8997-ab90c5425b05",
   "metadata": {},
   "outputs": [],
   "source": [
    "import PIL\n",
    "\n",
    "path = \"images/image.jpeg\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59bd84ef-8400-4822-bfc2-14a711ec61eb",
   "metadata": {},
   "source": [
    "## 读取图像\n",
    "\n",
    "Pillow 中最为重要的是Image类，可以通过Image模块的open函数来读取图像并获得Image类型的对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "51fd7db2-2188-48cb-81e0-acfea3c351f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "JPEG\n",
      "(1242, 1242)\n",
      "RGB\n"
     ]
    }
   ],
   "source": [
    "from PIL import Image\n",
    "\n",
    "# 读取图像获得Image对象\n",
    "image = Image.open(path)\n",
    "# 通过Image对象的format属性获得图像的格式\n",
    "print(image.format) # JPEG\n",
    "# 通过Image对象的size属性获得图像的尺寸\n",
    "print(image.size)  \n",
    "# 通过Image对象的mode属性获取图像的模式\n",
    "print(image.mode)   # RGB\n",
    "# 通过Image对象的show方法显示图像\n",
    "image.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e3f59f4-2424-4468-88f7-cfa2174915a2",
   "metadata": {},
   "source": [
    "## 裁剪图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c3da2152-0352-441f-95c6-20ebaca66243",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import matplotlib.pyplot as plt\n",
    "# img = plt.imread(path)\n",
    "# plt.imshow(img)\n",
    "# 通过Image对象的crop方法指定剪裁区域剪裁图像\n",
    "image.crop((200, 200, 800, 800)).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5389b488-58bf-40e3-a942-c3d802c297ee",
   "metadata": {},
   "source": [
    "## 生成略缩图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bc00e1fd-7bfb-489d-b961-4296d5879694",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过Image对象的thumbnail方法生成指定尺寸的缩略图\n",
    "image.thumbnail((128, 128))\n",
    "image.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dbcc301-5c5d-4546-b5b6-43dece71af16",
   "metadata": {},
   "source": [
    "## 缩放和粘贴图像"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a872d87-fa89-4ef9-bb93-ac7fe791b8ee",
   "metadata": {},
   "source": [
    "## 旋转和翻转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0da6aeff-4296-4e7f-b208-52a96f32cc3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = Image.open(path)\n",
    "# 使用Image对象的rotate方法实现图像的旋转\n",
    "image.rotate(45).show()\n",
    "# 使用Image对象的transpose方法实现图像翻转\n",
    "# Image.FLIP_LEFT_RIGHT - 水平翻转\n",
    "# Image.FLIP_TOP_BOTTOM - 垂直翻转\n",
    "image.transpose(Image.FLIP_TOP_BOTTOM).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57fb19fb-fbdf-46e4-9c79-dbfba5bda4cd",
   "metadata": {},
   "source": [
    "## 操作像素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "577c084b-426f-4eae-b095-9f12659c003e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for x in range(0, 310):\n",
    "    for y in range(0, 360):\n",
    "        # 通过Image对象的putpixel方法修改图像指定像素点\n",
    "        # 将像素点全换为白色\n",
    "        image.putpixel((x, y), (255, 255, 255))\n",
    "image.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a2a6a66-e056-4cba-b5d6-286f6be8e64d",
   "metadata": {},
   "source": [
    "## 滤镜（类似于素描）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "33fe4d85-175c-473f-832a-d045777d333e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import ImageFilter\n",
    "\n",
    "# 使用Image对象的filter方法对图像进行滤镜处理\n",
    "# ImageFilter模块包含了诸多预设的滤镜也可以自定义滤镜\n",
    "image.filter(ImageFilter.CONTOUR).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54dbfd08-7f38-41c2-904d-ccce35a0afe2",
   "metadata": {},
   "source": [
    "## 绘图\n",
    "\n",
    "Pillow 中有一个名为ImageDraw的模块，该模块的Draw函数会返回一个ImageDraw对象，通过ImageDraw对象的arc、line、rectangle、ellipse、polygon等方法，可以在图像上绘制出圆弧、线条、矩形、椭圆、多边形等形状，也可以通过该对象的text方法在图像上添加文字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4519ecfd-874d-4dd9-90e4-c8049f866eaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "from PIL import Image, ImageDraw, ImageFont\n",
    "\n",
    "\n",
    "def random_color():\n",
    "    \"\"\"生成随机颜色\"\"\"\n",
    "    red = random.randint(0, 255)\n",
    "    green = random.randint(0, 255)\n",
    "    blue = random.randint(0, 255)\n",
    "    return red, green, blue\n",
    "\n",
    "\n",
    "width, height = 800, 600\n",
    "# 创建一个800*600的图像，背景色为白色\n",
    "image = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))\n",
    "# 创建一个ImageDraw对象\n",
    "drawer = ImageDraw.Draw(image)\n",
    "# 通过指定字体和大小获得ImageFont对象\n",
    "font = ImageFont.truetype('pdf/font.ttf', 32)\n",
    "# 通过ImageDraw对象的text方法绘制文字\n",
    "drawer.text((300, 50), 'Hello, world!', fill=(255, 0, 0), font=font)\n",
    "# 通过ImageDraw对象的line方法绘制两条对角直线\n",
    "drawer.line((0, 0, width, height), fill=(0, 0, 255), width=2)\n",
    "drawer.line((width, 0, 0, height), fill=(0, 0, 255), width=2)\n",
    "xy = width // 2 - 60, height // 2 - 60, width // 2 + 60, height // 2 + 60\n",
    "# 通过ImageDraw对象的rectangle方法绘制矩形\n",
    "drawer.rectangle(xy, outline=(255, 0, 0), width=2)\n",
    "# 通过ImageDraw对象的ellipse方法绘制椭圆\n",
    "for i in range(4):\n",
    "    left, top, right, bottom = 150 + i * 120, 220, 310 + i * 120, 380\n",
    "    drawer.ellipse((left, top, right, bottom), outline=random_color(), width=8)\n",
    "# 显示图像\n",
    "image.show()\n",
    "# 保存图像\n",
    "image.save('images/result.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "178bd6bf-0de1-4374-83d8-6bab46828cbb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
